<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>4.Cubehelix 色盘</title>
    <style>
        canvas{
            border:1px solid #aaa;
            margin: 0 auto;
            display: block;
        }
    </style>
</head>
<body>
    <canvas width="800" height="800"></canvas>
    
    <script type="module">
        
        import {cubehelix} from '/common/lib/color/cubehelix/index.js';

        const canvas = document.querySelector('canvas');
        const ctx = canvas.getContext('2d');

        ctx.translate(0, 400);
        ctx.scale(1, -1);

        const color = cubehelix(); // 构造cubehelix色盘颜色映射函数
        const T = 2000;

        function update(t) {
            // 模拟数据按照正弦曲线变化
            const p = 0.5 + 0.5 * Math.sin(t / T);
            ctx.clearRect(0, -400, 512, 512);
            // 色盘颜色映射函数的参数取值范围是0-1
            const {r, g, b} = color(p);
            ctx.fillStyle = `rgb(${255 * r},${255 * g},${255 * b})`;
            ctx.beginPath();
            ctx.rect(20, -100, 480 * p, 40);
            ctx.fill();
            window.ctx = ctx;
            requestAnimationFrame(update);
        }

        update(0);

    </script>
</body>
</html>